using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace InventorySystem.UI
{
    // UI物品栏界面类
    public class UI_Inventory : MonoBehaviour
    {
        // 物品槽预制体
        [SerializeField]
        private GameObject _inventorySlotPrefab;
        // 对应的物品栏
        [SerializeField]
        private Inventory _inventory;
        // 物品槽列表
        [SerializeField]
        private List<UI_InventorySlot> _slots;
        
        // 物品栏属性
        public Inventory Inventory => _inventory;

        // 初始化物品栏UI
        [ContextMenu("初始化库存")]
        private void InitializeInventoryUi()
        {
            if (_inventory == null || _inventorySlotPrefab == null) 
                return;
            
            _slots = new List<UI_InventorySlot>(_inventory.Size);
            for (var i = 0; i < _inventory.Size; i++)
            {
                // 实例化物品槽预制体
                var uiSlot = Instantiate(_inventorySlotPrefab, transform);
                var uiSlotScript = uiSlot.GetComponent<UI_InventorySlot>();
                uiSlotScript.AssignSlot(i);
                // 将实例化生成的物品槽脚本添加到列表中
                _slots.Add(uiSlotScript);
            }
        }
    }
}
